const { list, find, add } = require('./data/shop-movie')
const base = '/shopmovie'

module.exports = [{
  url: `${base}/movies`,
  type: 'get',
  response: config => {
    const { page, limit, name, author, tag, minPlayers, maxPlayers, minDuration, maxDuration, orgUnitId } = config.query
    return list(page, limit, name, author, tag, minPlayers, maxPlayers, minDuration, maxDuration, orgUnitId)
  }
}, {
  url: `${base}/movies`,
  type: 'post',
  response: config => {
    add(config.body)
    return { success: true }
  }
}, {
  url: `${base}/movies/[1-9][0-9]*/online`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      r.status = 0
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/movies/[1-9][0-9]*/offline`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      r.status = 1
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/movies/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    const { price } = config.body
    if (r) {
      r.price = parseFloat(price)
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}]
